var fs = require('fs');
var mysql = require('mysql');
var superagent = require('superagent');
var cheerio = require('cheerio');

require('superagent-proxy')(superagent);

// cloudmusic
var mysqlConfig1 = require('./config').mysqlConfig1;
// ip
var mysqlConfig2 = require('./config').mysqlConfig2;

var ipList = [];
var ipIndex = 0;
var proxy = '';

function changeIp() {
    if (ipIndex < ipList.length - 1) {
        ipIndex += 1;
    } else {
        ipIndex = 0;
    }
    setProxy();
}

function setProxy() {
    var item = ipList[ipIndex];
    console.log(item);

    var ip = item.ip;
    var port = item.port;

    proxy = `http://${ip}:${port}`;
    console.log('proxy');

    console.log(proxy);

}
function getIpList() {
    var connection1 = mysql.createConnection(mysqlConfig2);
    var query1 = 'select * from ip_table1 where (success_count>5 and error_count/success_count<0.8 )';
    connection1.connect();
    connection1.query(query1, function (err, res) {
        if (err) {
            console.log('query1 error');
            setTimeout(() => {
                getIpList();
            }, 3000);
        } else {
            ipList = res;
            if (ipList.length > 0) {
                console.log('ip count');
                console.log(ipList.length);

                setProxy();
                setInterval(() => {
                    changeIp();
                }, 300 * 1000);
                start();
            } else {
                console.log('no ip');

            }
        }
    });
    connection1.end();
}
getIpList();
function start() {
    for (var i = 0; i < 20; i++) {
        var offset = 20 * i;
        getAlbumList(10, offset);
    }
}
function getAlbumList(limit, offset) {
    var albumList = [];
    var albumIndex = 0;

    var songList = [];

    var connection2 = mysql.createConnection(mysqlConfig1);
    var query2 = 'select * from album where (ISNULL( song_updated )) limit ' + limit + ' offset ' + offset;
    console.log(query2);

    connection2.query(query2, function (err, res) {
        if (err) {
            console.log('query2 err');

        } else {
            console.log('query2 success');
            if (res.length > 0) {
                albumList = res;
                albumCount = albumList.length;
                // console.log(albumList);
                // console.log(albumList.length);


                getSongList();
                function getSongList() {
                    var album = albumList[albumIndex];
                    // console.log(album);
                    var albumId = album.id;
                    var artistId = album.artist_id;

                    var url = `https://music.163.com/album?id=${albumId}`;
                    superagent.get(url)
                        .timeout(5000)
                        .proxy(proxy)
                        .end(function (err2, res2) {
                            console.log('http end');
                            console.log(albumId);

                            if (err2) {
                                console.log('http err');
                                changeIp();
                                getSongList();
                            } else {
                                console.log('http success');

                                var text = res2.text;
                                // console.log(url);

                                // fs.writeFile('album/album' + albumList[albumIndex].id + '.html', text, function () { })
                                var $ = cheerio.load(text);

                                var songListElement = $('#song-list-pre-cache');
                                var list2 = songListElement.find('.f-hide>li');
                                // console.log('list2 length');

                                // console.log(list2.length);


                                list2.each((i, element) => {
                                    var item = list2.eq(i);
                                    // var songId=item.
                                    // console.log('i: ' + i);

                                    var href = item.find('a').eq(0).attr().href;
                                    // console.log(href);
                                    var id = Number(href.replace(/\/song\?id=/g, ''));
                                    // console.log(id);


                                    var songName = item.text();
                                    // console.log('歌名: ');
                                    // console.log(songName);
                                    // console.log();
                                    var song = [id, songName, artistId, albumId];
                                    console.log(song);

                                    songList.push(song);
                                });
                                if (albumIndex < albumCount - 1) {
                                    albumIndex += 1;
                                    getSongList();
                                } else {
                                    // console.log(songList);
                                    function upload(songList) {
                                        var connection3 = mysql.createConnection(mysqlConfig1);
                                        connection3.connect();
                                        var query3 = 'insert ignore into song(id,name,artist_id,album_id) values ?';
                                        connection3.query(query3, [songList], function (err3, res3) {
                                            console.log('query3 end');

                                            if (err) {
                                                console.log('query3 err');
                                                console.log(err3);

                                            } else {
                                                console.log('query success');

                                                console.log(res3);
                                                var idList = albumList.map(v => {
                                                    return v.id;
                                                }).join(',');

                                                function feedback() {

                                                    var connection4 = mysql.createConnection(mysqlConfig1);
                                                    var query4 = 'update album set song_updated=1 where id in (' + idList + ');'
                                                    connection4.connect();
                                                    connection4.query(query4, function (err4, res4) {
                                                        console.log('query4 end');
                                                        if (err4) {
                                                            console.log('query4 err');

                                                        } else {
                                                            console.log('query4 success');
                                                            console.log(res4);
                                                            console.log('feedback end');
                                                            getAlbumList(limit, offset);
                                                            // setTimeout(() => {

                                                            // }, 2000);
                                                        }
                                                    });
                                                    connection4.end();
                                                }
                                                feedback();
                                            }
                                        });
                                        connection3.end();
                                    }
                                    upload(songList);
                                }
                            }
                        })
                }
            } else {
                console.log('no album');
                console.log(query2);

            }
        }
    });
    connection2.end();
}
